﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AMing.Api.Model;
using AMing.Api.Extensions;
using AMing.Extension.Net40;

namespace AMing.Api.Controllers
{
    public class KeyValueController : Controller
    {
        //
        // GET: /KeyValue/

        [HttpGet]
        public ActionResult Index(bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.List();

                if (result != null)
                {
                    foreach (var item in result)
                    {
                        item.Value = HttpUtility.UrlDecode(item.Value);
                    }
                }

                return this.Jsonp(result);
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(new string[] { });
        }


        [HttpGet]
        public ActionResult Get(string key, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Get(key);
                if (result != null && result.Count > 0)
                {
                    var val = result[0];
                    val.Value = HttpUtility.UrlDecode(val.Value);

                    return this.Jsonp(val);
                }
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(null);
        }

        [HttpGet]
        public ActionResult Value(string key, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Get(key);
                if (result != null && result.Count > 0)
                {
                    string val = HttpUtility.UrlDecode(result[0].Value);
                    return this.Jsonp(val, false);
                }
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(null);
        }

        [HttpGet]
        public ActionResult Exist(string key, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Exist(key);

                return this.Jsonp(result);
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(false);
        }

        public ActionResult Add(string key, string value, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Exist(key);
                if (result)
                {
                    throw new Exception("key exist.");
                }

                result = keyvalueHelper.Add(new KeyValue { Key = key, Value = HttpUtility.UrlEncode(value) });

                return this.Jsonp(result);
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(false);
        }

        public ActionResult Update(string key, string value, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Exist(key);
                if (!result)
                {
                    throw new Exception("key does not exist.");
                }

                result = keyvalueHelper.Update(new KeyValue { Key = key, Value = HttpUtility.UrlEncode(value) });

                return this.Jsonp(result);
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(false);
        }

        public ActionResult AddOrUpdate(string key, string value, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Exist(key);
                if (result)
                {
                    result = keyvalueHelper.Update(new KeyValue { Key = key, Value = HttpUtility.UrlEncode(value) });
                }
                else
                {
                    result = keyvalueHelper.Add(new KeyValue { Key = key, Value = HttpUtility.UrlEncode(value) });
                }

                return this.Jsonp(result);
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(false);
        }

        public ActionResult Delete(string key, bool showError = false)
        {
            try
            {
                Data.KeyValueHelper keyvalueHelper = new Data.KeyValueHelper();
                var result = keyvalueHelper.Exist(key);
                if (!result)
                {
                    throw new Exception("key does not exist.");
                }

                result = keyvalueHelper.Delete(key);

                return this.Jsonp(result);
            }
            catch (Exception ex)
            {
                if (showError)
                {
                    return this.Jsonp(ex.Message);
                }
            }

            return this.Jsonp(false);
        }


    }
}
